package com.tbit.uqbike.webmanager.business.machine_op_log.controller;

import com.alibaba.fastjson.JSON;
import com.tbit.common.entity.MachineOpLog;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.object.business.PageData;
import com.tbit.uqbike.object.pojo.OrderLog;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.webmanager.business.machine_op_log.object.dto.MachineOpLogPageReq;
import com.tbit.uqbike.webmanager.business.machine_op_log.service.MachineOpLogService;
import com.tbit.uqbike.webmanager.dto.MachineOpLogDTO;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆操作日志
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2024/11/18
 */
@Slf4j
@RestController
@RequestMapping("/machineOpLog")
@RequiredArgsConstructor
public class MachineOpLogController {
    final MachineOpLogService machineOpLogService;

    /**
     * 分页查询车辆操作日志
     *
     * @param req 查询参数
     */
    @GetMapping("/getPage")
    public Result<PageResult<MachineOpLog>> getPage(@Validated MachineOpLogPageReq req) {
        if (req.getMachineNO() == null && req.getUserCode() == null) {
            return Result.error("请输入车辆编号或设备编号后查询");
        }
        return Result.success(machineOpLogService.getRecordList(req));
    }

    /**
     * @description: 导出车辆操作日志
     * @author: mo.shanyong
     * @date: 2025/3/17 15:31
     * @param: req
     * @return: com.tbit.common.entity.view.Result
     */
    @PostMapping("/exportInfo")
    public Result<Object> exportInfo(@Validated MachineOpLogPageReq req) {
        if (req.getMachineNO() == null && req.getUserCode() == null) {
            return Result.error("请输入车辆编号或设备编号后查询");
        }
        req.setPageNO(1);
        //查询总数
        int count = machineOpLogService.getCount(req);
        req.setRowCount(count == 0 ? 10 : count);
        log.info("exportInfo----->{}", JSON.toJSONString(req));
        PageResult<MachineOpLog> recordList = machineOpLogService.getRecordList(req);
        List<MachineOpLogDTO> machineOpLogDTOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(recordList.getList())) {
            BeanUtils.copyProperties(recordList.getList(), machineOpLogDTOList);
        }
        ExcelUtil<MachineOpLogDTO> excel = new ExcelUtil<>(MachineOpLogDTO.class);
        JsonResult jsonResult = excel.exportExcel(machineOpLogDTOList, "车辆操作日志");
        return Result.success(jsonResult.getData());
    }
}
